package com.example.administrator.testdagger.tview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.icu.util.Calendar;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.administrator.testdagger.R;
import com.example.administrator.testdagger.exception.NoDetermineSizeException;

/**
 * Description: 
 * ModelName:
 *
 * @author WangJQ
 * Create at:  2018/6/5 15:30
 * Company: 沈阳艾尔时代科技发展有限公司
 * Copyright: (c)2018 AIR Times Inc. All rights reserved.
 * @version 1.0
 */
public class CustomClock extends View {
    /**
     * 外圆半径
     */
    private float mRadius;
    /**
     * 边距
     */
    private float mPadding;
    /**
     * 文字大小
     */
    private float mTextSize;
    /**
     * 时针/分针/秒针宽度
     */
    private float mHourPointWidth;
    private float mMinPointWidth;
    private float mSecondPointWidth;
    /**
     * 指针圆角
     */
    private int mPointRadius;
    /**
     * 指针末尾的长度
     */
    private float mPointEndLength;

    /**
     * 长线/短线的颜色
     */
    private int mColorLong;
    private int mColorShort;
    /**
     * 时针/分针/秒针的颜色
     */
    private int mHourPointColor;
    private int mMinPointColor;
    private int mSecondColor;

    /**
     * 画笔
     */
    private Paint mPaint;


    public CustomClock(Context context) {
        this(context, null);
    }

    public CustomClock(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomClock(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyledAttrs(attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        //防抖动
        mPaint.setDither(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设定一个最小值
        int width = 5000;

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED
                || heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            try {
                throw new NoDetermineSizeException("宽度和高度至少有一个确定的值，不能同时为wrap_content");
            } catch (NoDetermineSizeException e) {
                e.printStackTrace();
            }
        } else {
            if (widthMode == MeasureSpec.EXACTLY) {
                width = Math.min(widthSize, width);
            }

            if (heightMode == MeasureSpec.EXACTLY) {
                width = Math.min(heightSize, width);
            }
        }

        setMeasuredDimension(width, width);
    }

    private static final String TAG = "CustomClock";

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.d(TAG, "onSizeChanged");
        mRadius = (Math.min(w, h) - mPadding) / 2;
        mPointEndLength = mRadius / 6;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getWidth() / 2, getHeight() / 2);
        paintCircle(canvas);
        paintScale(canvas);
        paintPoint(canvas);
        canvas.restore();
        //刷新
        postInvalidateDelayed(1000);

    }

    /**
     * 绘制外圆背景
     * @param canvas
     */
    public void paintCircle(Canvas canvas) {
        mPaint.setColor(Color.WHITE);
        canvas.drawCircle(0, 0, mRadius, mPaint);
    }

    public void paintScale(Canvas canvas) {
        mPaint.setStrokeWidth(2);
        canvas.save();
        int lineWidth = 0;
        for (int i = 0; i < 60; i++) {
            if (i % 5 == 0) {
                mPaint.setStrokeWidth(3f);
                mPaint.setColor(mColorLong);
                lineWidth = 80;

                mPaint.setTextSize(60);
                String text = ((i / 5) == 0 ? 12 : (i / 5)) + "";
                Rect textBound = new Rect();
                mPaint.getTextBounds(text, 0, text.length(), textBound);
                mPaint.setColor(Color.BLACK);
                canvas.save();
                canvas.translate(0, -mRadius + 20 + lineWidth + (textBound.bottom - textBound.top));
                canvas.rotate(-6 * i);
                mPaint.setStyle(Paint.Style.FILL);
                canvas.drawText(text, -(textBound.right - textBound.left) / 2, textBound.bottom, mPaint);
                canvas.restore();
            } else {
                mPaint.setStrokeWidth(2);
                mPaint.setColor(mColorShort);
                lineWidth = 60;
            }
            canvas.drawLine(0, -mRadius + 10, 0, -mRadius + 10 + lineWidth, mPaint);
            canvas.rotate(6);
        }
        canvas.restore();
    }

    private void paintPoint(Canvas canvas) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY); //时
            int minute = calendar.get(Calendar.MINUTE); //分
            int second = calendar.get(Calendar.SECOND); //秒
            int angleHour = (hour % 12) * 360 / 12; //时针转过的角度
            int angleMinute = minute * 360 / 60; //分针转过的角度
            int angleSecond = second * 360 / 60; //秒针转过的角度
            //绘制时针
            canvas.save();
            canvas.rotate(angleHour); //旋转到时针的角度
            RectF rectFHour = new RectF(-mHourPointWidth / 2, -mRadius * 3 / 5, mHourPointWidth / 2, mPointEndLength);
            mPaint.setColor(Color.BLACK); //设置指针颜色
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(mHourPointWidth); //设置边界宽度
            canvas.drawRoundRect(rectFHour, mPointRadius, mPointRadius, mPaint); //绘制时针
            canvas.restore();
            //绘制分针
            canvas.save();
            canvas.rotate(angleMinute);
            RectF rectFMinute = new RectF(-mMinPointWidth / 2, -mRadius * 3.5f / 5, mMinPointWidth / 2, mPointEndLength);
            mPaint.setColor(Color.BLACK);
            mPaint.setStrokeWidth(mMinPointWidth);
            canvas.drawRoundRect(rectFMinute, mPointRadius, mPointRadius, mPaint);
            canvas.restore();
            //绘制秒针
            canvas.save();
            canvas.rotate(angleSecond);
            RectF rectFSecond = new RectF(-mSecondPointWidth / 2, -mRadius + 15, mSecondPointWidth / 2, mPointEndLength);
            mPaint.setColor(mSecondColor);
            mPaint.setStrokeWidth(mSecondPointWidth);
            canvas.drawRoundRect(rectFSecond, mPointRadius, mPointRadius, mPaint);
            canvas.restore();
            //绘制中心小圆
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setColor(mSecondColor);
            canvas.drawCircle(0, 0, mSecondPointWidth * 4, mPaint);
        }
    }

    private void obtainStyledAttrs(AttributeSet attrs) {
        TypedArray array = null;
        try {
            array = getContext().obtainStyledAttributes(attrs, R.styleable.WatchBoard);
            mPadding = array.getDimension(R.styleable.WatchBoard_wb_padding, DptoPx(10));
            mTextSize = array.getDimension(R.styleable.WatchBoard_wb_text_size, SptoPx(16));
            mHourPointWidth = array.getDimension(R.styleable.WatchBoard_wb_hour_pointer_width, DptoPx(5));
            mMinPointWidth = array.getDimension(R.styleable.WatchBoard_wb_minute_pointer_width, DptoPx(3));
            mSecondPointWidth = array.getDimension(R.styleable.WatchBoard_wb_second_pointer_width, DptoPx(2));
            mPointRadius = (int) array.getDimension(R.styleable.WatchBoard_wb_pointer_corner_radius, DptoPx(10));
            mPointEndLength = array.getDimension(R.styleable.WatchBoard_wb_pointer_end_length, DptoPx(10));

            mColorLong = array.getColor(R.styleable.WatchBoard_wb_scale_long_color, Color.argb(225, 0, 0, 0));
            mColorShort = array.getColor(R.styleable.WatchBoard_wb_scale_short_color, Color.argb(125, 0, 0, 0));
            mMinPointColor = array.getColor(R.styleable.WatchBoard_wb_minute_pointer_color, Color.BLACK);
            mSecondColor = array.getColor(R.styleable.WatchBoard_wb_second_pointer_color, Color.RED);
        } catch (Exception e) {
            //一旦出现错误全部使用默认值
            mPadding = DptoPx(10);
            mTextSize = 80;
            mHourPointWidth = DptoPx(5);
            mMinPointWidth = DptoPx(3);
            mSecondPointWidth = DptoPx(2);
            mPointRadius = (int) DptoPx(10);
            mPointEndLength = DptoPx(10);

            mColorLong = Color.argb(225, 0, 0, 0);
            mColorShort = Color.argb(125, 0, 0, 0);
            mMinPointColor = Color.BLACK;
            mSecondColor = Color.RED;
            mHourPointColor = Color.BLACK;
        } finally {
            if (array != null) {
                array.recycle();
            }
        }

    }


    //Dp转px
    private float DptoPx(int value) {

        return value;
    }

    //sp转px
    private float SptoPx(int value) {
        return value;
    }
}
